import java.util.Arrays;

/**
 * @ClassName LC59
 * 螺旋矩阵
 * @Author ykx
 * @Date 2024/3/27 18:24
 * @Version 1.0
 */
public class LC59 {
    public static void main(String[] args) {
        LC59 lc59 = new LC59();
        System.out.println(Arrays.deepToString(lc59.generateMatrix(9)));
    }

    public int[][] generateMatrix(int n) {
        int[][] result = new int[n][n];
        int start = 1;
        int end = n * n;
        // 第几次遍历
        int count = 0;
        // 第几圈
        int cycle = 0;
        int row = 0;
        while (cycle <= (n + 1) / 2) {
            cycle = count / 4;
            switch (count % 4) {
                case 0:
                    start = fillResultLeftToRight(result, cycle, start);
                    break;
                case 1:
                    start = fillResultUpToDown(result, cycle, start);
                    break;
                case 2:
                    start = fillResultRightToLeft(result, cycle, start);
                    break;
                case 3:
                    start = fillResultDownToUp(result, cycle, start);
                    break;
            }
            count++;
        }

        if (n % 2 != 0) {
            result[n / 2][n / 2] = end;
        }

        return result;
    }

    /**
     * 从左向右填充
     *
     * @param result 结果
     * @param row    第几行
     * @param start  起始元素
     */
    public int fillResultLeftToRight(int[][] result, int row, int start) {
        int n = result.length;
        for (int j = row; j < n - 1 - row; j++) {
            result[row][j] = start++;
        }
        return start;
    }

    /**
     * 从右向左填充
     *
     * @param result 结果
     * @param row    第几圈
     * @param start  起始元素
     */
    public int fillResultRightToLeft(int[][] result, int row, int start) {
        int n = result.length;
        for (int j = n - 1 - row; j > row; j--) {
            result[n - 1 - row][j] = start++;
        }
        return start;
    }

    /**
     * 从上往下填充
     *
     * @param result 结果
     * @param col    第几圈
     * @param start  起始位置
     */
    public int fillResultUpToDown(int[][] result, int col, int start) {
        int n = result.length;
        for (int j = col; j < n - 1 - col; j++) {
            result[j][n - 1 - col] = start++;
        }
        return start;
    }

    /**
     * 从下往上填充
     *
     * @param result 结果
     * @param col    第几圈
     * @param start  起始位置
     */
    public int fillResultDownToUp(int[][] result, int col, int start) {
        int n = result.length;
        for (int j = n - 1 - col; j > col; j--) {
            result[j][col] = start++;
        }
        return start;
    }

    /*
    0:1-8,1:9-16,2:17-24,3:25-32(index:0)
    4:33-38,5:39-(index:1)
     01 02 03 04 05 06 07 08 09
     32 33 34 35 36 37 38 39 10
     31 56 57 58 59 60 61 40 11
     30 55 72 73 74 75 62 41 12
     29 54 71 80 81 76 63 42 13
     28 53 70 79 78 77 64 43 14
     27 52 69 68 67 66 65 44 15
     26 51 50 49 48 47 46 45 16
     25 24 23 22 21 20 19 18 17
     */

}
